package mt2118;
import java.util.PriorityQueue;
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		//优先队列，默认小的先出
	    PriorityQueue<Integer> queue = new PriorityQueue<>();
	    int n = scan.nextInt();
	    for(int i = 0;i<n;i++) {
	        queue.add(scan.nextInt());
	    }
	    int sum = 0;
	    //每次找队列中最小的两个合并，再入队列
	    //Huffman树编码思想
	    while(queue.size() > 1) {
	        int a = queue.poll();
	        int b = queue.poll();
	        sum += a+b;
	        queue.add(a+b);
	    }
	    System.out.println(sum);
	  scan.close();
	}
}
